using System;

namespace LFramework 
{
    public class LoopTimer : TimerBase
    {
        private float m_LoopTime;
        private Action m_CB;

        private float m_TimeCount = 0f;

        public static LoopTimer Create(int id, float loop_time, Action cb)
        {
            LoopTimer frameTimer = GameEntry.Pool.ClassObjectPool.Dequeue<LoopTimer>();
            frameTimer.Init(id, loop_time, cb);
            return frameTimer;
        }

        public void Init(int id, float loop_time, Action cb)
        {
            ID = id;
            m_LoopTime = loop_time;
            m_CB = cb;
            m_TimeCount = 0f;
        }

        public override void Update(float dt)
        {
            m_LoopTime += dt;

            while (m_TimeCount >= m_LoopTime)
            {
                m_TimeCount -= m_LoopTime;
                m_CB?.Invoke();
            }
        }

        public override void Restart()
        {
            m_LoopTime = 0;
        }
    }
}

